package org.jeecg.modules.my.cblOrder.service.impl;

import org.jeecg.modules.my.cblOrder.entity.CblOrder;
import org.jeecg.modules.my.cblOrder.entity.CblOrderDetail;
import org.jeecg.modules.my.cblOrder.mapper.CblOrderDetailMapper;
import org.jeecg.modules.my.cblOrder.mapper.CblOrderMapper;
import org.jeecg.modules.my.cblOrder.service.ICblOrderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 客户提交给供应商的采购单
 * @Author: jeecg-boot
 * @Date:   2020-09-06
 * @Version: V1.0
 */
@Service
public class CblOrderServiceImpl extends ServiceImpl<CblOrderMapper, CblOrder> implements ICblOrderService {

	@Autowired
	private CblOrderMapper cblOrderMapper;
	@Autowired
	private CblOrderDetailMapper cblOrderDetailMapper;
	
	@Override
	@Transactional
	public void saveMain(CblOrder cblOrder, List<CblOrderDetail> cblOrderDetailList) {
		cblOrderMapper.insert(cblOrder);
		if(cblOrderDetailList!=null && cblOrderDetailList.size()>0) {
			for(CblOrderDetail entity:cblOrderDetailList) {
				//外键设置
				entity.setZhubId(cblOrder.getId());
				cblOrderDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(CblOrder cblOrder,List<CblOrderDetail> cblOrderDetailList) {
		cblOrderMapper.updateById(cblOrder);
		
		//1.先删除子表数据
		cblOrderDetailMapper.deleteByMainId(cblOrder.getId());
		
		//2.子表数据重新插入
		if(cblOrderDetailList!=null && cblOrderDetailList.size()>0) {
			for(CblOrderDetail entity:cblOrderDetailList) {
				//外键设置
				entity.setZhubId(cblOrder.getId());
				cblOrderDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		cblOrderDetailMapper.deleteByMainId(id);
		cblOrderMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			cblOrderDetailMapper.deleteByMainId(id.toString());
			cblOrderMapper.deleteById(id);
		}
	}
	
}
